package com.mathew.spring.appsp.controller;

import com.mathew.spring.appsp.model.Book;
import com.mathew.spring.appsp.model.BookService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

@RestController
public class BookController {


  BookService bookService;

  @Autowired
  BookController(BookService bookService) {
    this.bookService = bookService;
  }

  @GetMapping(value = "/batis")
  public int getBatis(String name,String author) {

    Book book = new Book();
    book.setName(name);
    book.setAuthor(author);

    try {
      return bookService.addBook(book);
    } catch (Exception e) {
      return e.hashCode();
    }
  }
  @PostMapping(value = "/insert",consumes = MediaType.APPLICATION_JSON_VALUE)
  public int addBook(@RequestBody Book book) {
    return bookService.addBook(book);
  }

  @GetMapping(value = "/deleteById/{id}")
  public int deleteById(@PathVariable Integer id) {
    return bookService.deleteById(id);
  }

  @GetMapping(value = "/selectBook/{id}")
  public Book selectBook(@PathVariable Integer id) {
    Book book = bookService.selectBook(id);
    System.out.println(book.toString());
    return book;
  }
  @GetMapping(value = "/selectBookNameById/{id}")
  public String selectBookNameById(@PathVariable Integer id) {
    return bookService.selectBookName(id);
  }

}
